package assistant.dao.daoImpl;

import assistant.dao.StudentCourseDao;
import assistant.domain.Evaluate;
import assistant.domain.StudentCourse;
import assistant.domain.Wish;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023/1/10 13:48
 * @Author lihu
 */
public class StudentCourseDaoImpl implements StudentCourseDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;

    @Override
    public void insert(StudentCourse studentCourse) {
        String sql = "insert into lh_student_course\n" +
                "        (student_id, course_id, select_time) \n" +
                "values(?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, studentCourse.getStudentId());
            psmt.setLong(2, studentCourse.getCourseId());
            psmt.setObject(3, studentCourse.getSelectTime());
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public void updateById(StudentCourse studentCourse) {
        String sql = "update lh_student_course set";
        List<Object> param = new ArrayList<>();
        if (studentCourse == null) {
            return;
        } else {
            if (studentCourse.getStudentId() != null) {
                sql += " student_id = ?,";
                param.add(studentCourse.getStudentId());
            }
            if (studentCourse.getCourseId() != null) {
                sql += " course_id= ?,";
                param.add(studentCourse.getCourseId());
            }
            if (studentCourse.getSelectTime() != null) {
                sql += " select_time = ?,";
                param.add(studentCourse.getSelectTime());
            }

        }
        sql = StrUtil.strip(sql, null, ",");
        sql += " where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            psmt.setLong(param.size() + 1, studentCourse.getId());
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from lh_student_course where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public StudentCourse selectById(Long id) {
        String sql = "select * from lh_student_course where id = ?";
        StudentCourse studentCourse = new StudentCourse();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            rs = psmt.executeQuery();
            while (rs.next()) {
                studentCourse.setId(rs.getLong(1));
                studentCourse.setStudentId(rs.getLong(2));
                studentCourse.setCourseId(rs.getLong(3));
                studentCourse.setSelectTime(rs.getTime(4));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return studentCourse;
    }

    @Override
    public List<StudentCourse> selectList(StudentCourse studentCourse) {
        String sql = "select * from lh_student_course where 1=1";
        List<StudentCourse> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (studentCourse == null) {
            sql = "select * from lh_student_course";
        } else {
            if (studentCourse.getId() != null) {
                sql += " and id = ?";
                param.add(studentCourse.getId());
            }
            if (studentCourse.getStudentId() != null) {
                sql += " and student_id = ?";
                param.add(studentCourse.getStudentId());
            }
            if (studentCourse.getCourseId() != null) {
                sql += " and course_id= ?";
                param.add(studentCourse.getCourseId());
            }
            if (studentCourse.getSelectTime() != null) {
                sql += " and select_time = ?";
                param.add(studentCourse.getSelectTime());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()) {
                StudentCourse studentCourse1 = new StudentCourse();
                studentCourse1.setId(rs.getLong(1));
                studentCourse1.setStudentId(rs.getLong(2));
                studentCourse1.setCourseId(rs.getLong(3));
                studentCourse1.setSelectTime(rs.getTime(4));
                list.add(studentCourse1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return list;
    }
}
